# config/database.py
import os
from dataclasses import dataclass, field
from typing import Optional
from dotenv import load_dotenv

# 加载.env文件
load_dotenv()

@dataclass
class DatabaseConfig:
    """PostgreSQL数据库配置"""
    host: str = field(default_factory=lambda: os.getenv('DB_HOST', 'localhost'))
    port: int = field(default_factory=lambda: int(os.getenv('DB_PORT', '5433')))
    database: str = field(default_factory=lambda: os.getenv('DB_NAME', 'stock_data'))
    username: str = field(default_factory=lambda: os.getenv('DB_USER', 'postgres'))
    password: str = field(default_factory=lambda: os.getenv('DB_PASSWORD', 'postgres'))
    pool_size: int = field(default_factory=lambda: int(os.getenv('DB_POOL_SIZE', '10')))
    max_overflow: int = field(default_factory=lambda: int(os.getenv('DB_MAX_OVERFLOW', '20')))
    echo_sql: bool = field(default_factory=lambda: os.getenv('DB_ECHO_SQL', 'false').lower() == 'true')

@dataclass
class BaostockConfig:
    """Baostock配置"""
    max_retries: int = field(default_factory=lambda: int(os.getenv('BAOSTOCK_MAX_RETRIES', '3')))
    request_delay: float = field(default_factory=lambda: float(os.getenv('BAOSTOCK_REQUEST_DELAY', '0.2')))
    timeout: int = field(default_factory=lambda: int(os.getenv('BAOSTOCK_TIMEOUT', '30')))

@dataclass
class AppConfig:
    """应用配置"""
    database: DatabaseConfig = field(default_factory=DatabaseConfig)
    baostock: BaostockConfig = field(default_factory=BaostockConfig)
    log_level: str = field(default_factory=lambda: os.getenv('LOG_LEVEL', 'INFO'))